<div class="sortable" id="items-424">
<h1>Edit SubProduct <?php echo $productName; ?></h1>
<?php $this->widget('Xpress.extensions.web.widgets.XGridView', array(
    'id'=>'subproduct-grid-'.$model->product_id,
    'ajaxVar'=>'subproduct',
    'summaryText'=>'Displaying {start}-{end} of {count} subproducts',
    'cssFile'=>false,
    'dataProvider'=>$model->search($lazyLoad),
    'filter'=>$model,
    'selectableRows'=>2,
    'htmlOptions' => array('class' => 'table table-hover'),
    'menu' => array(
        'items'=>array(
            array('label' => '<i class="icon-trash"></i> Delete selected items', 'url'=>$this->createUrl('/Shop/admin/subProduct/delete'),
                'linkOptions' => array('class'=>'lnk-delete-sub-products-multiply'),
                'visible'=>'$grid->dataProvider->ItemCount > 0',
            ),
        ),
        'encodeLabel'=>false,
    ),
    'columns'=>array(
        array(
            'class'=>'CCheckBoxColumn',
            'value'=>'$data->id',
            'htmlOptions'=>array('width'=>'3%'),
        ),
        array(
            'name'=>'sku',
            'value'=>'$data->sku',
        ),
        array(
            'header'=>'Size',
            'value'=>'(($size = $data->getAttributeByGroupName("Mattress size")) === null ? "" : $size->name)',
        ),
        array(
            'header'=>'Foundation',
            'value'=>'(($foundation = $data->getAttributeByGroupName("Foundation")) === null ? "" : $foundation->name)',
        ),
        array(
            'name'=>'status',
            'type'=>'raw',
            'value'=>'CHtml::link($data->status ? "<i class=\'icon-ok\'></i>" : "<i class=\'icon-ban-circle\'></i>", array("/Xpress/service/ajax", "SID"=>"Shop.SubProduct.changeStatus", "ids[]"=>$data->id, "value"=>$data->status ? 0 : 1), array("class"=>($data->status ? "active" : "")))',
            'filter'=>array('1'=>'Show','0'=>'Hide'),
            'htmlOptions'=>array(
                'class'=>'subproduct-status-column',
                'width'=>'80',
                'style'=>'text-align:center;',
            ),
            'headerHtmlOptions'=>array('style'=>'text-align: center;'),
        ),
        array(
            'header'=>'Default',
            'type'=>'raw',
            'value'=>'CHtml::link($data->id == $data->product->sub_default ? "<i class=\'icon-ok\'></i>" : "<i class=\'icon-ban-circle\'></i>", array("/Xpress/service/ajax", "SID"=>"Shop.Product.changeDefault", "subproductId"=>$data->id, "id"=>$data->product_id))',
            'filter'=>false,
            'htmlOptions'=>array(
                'class'=>'subproduct-default-column',
                'width'=>'80',
                'style'=>'text-align:center;',
            ),
            'headerHtmlOptions'=>array('style'=>'text-align: center;'),
        ),
        array(
            'header' => 'Price',
            'type' => 'raw',
            'value' => '$data->getNormalPriceColumnContent()',
            'htmlOptions'=>array('width'=>'15%','style'=>'text-align: right;'),
            'headerHtmlOptions'=>array('style'=>'text-align: right;'),
        ),
        array(
            'header' => 'Sale Price',
            'type' => 'raw',
            'value' => '$data->getSalePriceColumnContent()',
            'htmlOptions'=>array('width'=>'15%','style'=>'text-align: right;'),
            'headerHtmlOptions'=>array('style'=>'text-align: right;'),
        ),
        array(
            'class'=>'CButtonColumn',
            'template'=>'{update-subproduct} {delete} {preview}',
            'deleteButtonUrl'=>"url('/Shop/admin/subProduct/delete',array('id'=>\$data->id))",
            'deleteButtonImageUrl'=>false,
            'deleteButtonLabel'=>'<i class="icon-trash"></i>',
            'deleteButtonOptions'=>array('title'=>'Delete'),
//            'updateButtonUrl'=>"url('/Shop/admin/subProduct/update',array('id'=>\$data->id,'product_id'=>\$data->product_id))",
//            'updateButtonUrl'=>"'#'",
//            'updateButtonImageUrl'=>false,
//            'updateButtonLabel'=>'<i class="icon-pencil"></i>',
//            'updateButtonOptions'=>array('class' => 'update xtarget-detail', 'title'=>'Update'),
            'buttons'=>array(
                'preview' => array(
                    'label' => '<i class="icon-eye-open"></i>',
                    'url' => 'Yii::app()->controller->previewUrl($data)',
                    'options' => array('class' => 'preview','title' => 'Preview','target'=>'_blank')
                ),
                'update-subproduct' => array(
                    'label' => '<i class="icon-pencil"></i>',
                    'url' => "url('/Shop/admin/subProduct/update',array('id'=>\$data->id,'product_id'=>\$data->product_id, 'productName'=>'".$productName."','layout'=>'//layouts/popup1'))",
                    'options' => array('class' => 'icon-pencil','title' => 'Edit')
                ),
            )
        ),
    ),
));
$js = <<<EOP
$('body').on('click','.lnk-delete-sub-products-multiply',function(){
    var href = $(this).attr('href'), id = [], grid = $(this).closest('.table').attr('id');

    $.each($(this).closest('.table').find('.items .select-on-check:checked'),function(){
        id.push($(this).val());
    });

    if (id.length <= 0)
    {
        alert('Please select at least an item !');
        return false;
    }
    if (!confirm('Are you sure you want to delete this subproducts?'))
        return false;

    id = id.join(',');
    href = $.param.querystring(href, {id:id});
    $.post(href, function(data){
        data = $.parseJSON(data);
        if (data != null)
        {
            //update subproduct grid
            $.fn.yiiGridView.update(grid);
            alert(data['msg']);
        }
    });
    return false;
});
EOP;
cs()->registerScript(__CLASS__.'#DeleteMultiSubProduct', $js);

cs()->registerScript(__CLASS__ . '#OpenPreview', "
$('body').on('click', '.preview', function(){
    if ($(this).attr('href') == '#')
    {
        alert('Can not preview this product')
        return false;
    }
});
");

$js = <<<EOP
$('body').on('click', '.subproduct-status-column > a', function(){
    var href = $(this).attr('href'),
        data = $.deparam.querystring(href),
        id = $(this).closest('.table').attr('id')
        ;
    $.post(href, data, function(){
        $.fn.yiiGridView.update(id);
    });
    return false;
});
EOP;
cs()->registerScript(__CLASS__.'#ChangeSubProductStatus', $js);

$js = <<<EOP
$('body').on('click', '.subproduct-default-column > a', function(){
    var href = $(this).attr('href');
    var data = $.deparam.querystring(href);
    var id = $(this).closest('.table').attr('id');
    $.post(href, data, function(){
        $.fn.yiiGridView.update(id);
    });
    return false;
});
EOP;
cs()->registerScript(__CLASS__.'#ChangeDefaultSubProduct', $js);

$js = <<<EOP
$('body').on('click', '.sortable .table .delete', function(){
    if (!confirm('Are you sure you want to delete this subproduct?')) return false;
    var href = $(this).attr('href');
    var data = $.deparam.querystring(href);
    var grid = $(this).closest('.table').attr('id');
    $.post(href, data, function(data){
        data = $.parseJSON(data);
        if (data != null)
        {
            $.fn.yiiGridView.update(grid);
            alert(data['msg']);
        }
    });
    return false;
});
EOP;
cs()->registerScript(__CLASS__.'#SubProductDelete', $js);

//$js = "
//parent.$('#fancybox-frame').off();
//$('body').on('click', '.sortable .table .update', function(){
//        var link = '/admin/index.php?r=Shop/admin/subProduct/update&id=26576&product_id=77';
//        parent.$('#fancybox-frame').attr('src', link);
//        return false;
//})";
//cs()->registerScript(__CLASS__.'#SubProductUpdate', $js);
?>
</div>
<div class="control-group buttons">
    <div class="controls" style="text-align: center">
        <input type="button" class="btn btn-primary productUpdate" value="Save" onclick="parent.refresh=1;parent.$.fancybox.close();">
    </div>
</div>